package top.scsoul.voes.examination.convert;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import top.scsoul.voes.common.vo.ExcelVo;
import top.scsoul.voes.examination.entity.QuestionBank;
import top.scsoul.voes.examination.entity.QuestionFillBlanks;
import top.scsoul.voes.examination.entity.QuestionJudgment;
import top.scsoul.voes.examination.entity.QuestionOption;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *
 * @author zhangjl
 * @date 2021/8/17 9:49
 */
public class BeanConvert {
    public static List<ExcelVo> questionListToExcelVoList(List<QuestionBank> questionBanks) {
        List<ExcelVo> excelVos = new ArrayList<>();
        for (QuestionBank item : questionBanks) {
            ExcelVo excelVo = new ExcelVo();
            QuestionFillBlanks questionFillBlank = item.getQuestionFillBlank();
            QuestionOption questionOption = item.getQuestionOption();
            QuestionJudgment questionJudgment = item.getQuestionJudgment();

            //拷贝本体和多选填空题数据
            BeanUtil.copyProperties(item,excelVo);
            BeanUtil.copyProperties(questionFillBlank,excelVo);
            BeanUtil.copyProperties(questionOption,excelVo);
            //拷贝判断题数据
            if(questionJudgment!=null){
                excelVo.setQuestionJudgmentExplain(questionJudgment.getExplain());
                excelVo.setQuestionJudgmentRightKey(questionJudgment.getRightKey());
            }
            excelVos.add(excelVo);
        }
        return excelVos;
    }

    public static List<QuestionBank> excelVoToQuestion(List<ExcelVo> excelVos) {
        List<QuestionBank> questionBanks = new ArrayList<>();

        for (ExcelVo excel : excelVos) {
            QuestionBank questionBank = new QuestionBank();
            BeanUtil.copyProperties(excel,questionBank);

            QuestionOption questionOption = new QuestionOption();
            BeanUtil.copyProperties(excel,questionOption);

            QuestionFillBlanks questionFillBlanks =null;
            if(excel.getAnswer()!=null){
                questionFillBlanks= new QuestionFillBlanks();
                BeanUtil.copyProperties(excel,questionFillBlanks);
            }

            QuestionJudgment questionJudgment=null ;
            if(excel.getQuestionJudgmentExplain()!=null||excel.getQuestionJudgmentRightKey()!=null){
                questionJudgment=new QuestionJudgment();
                questionJudgment.setExplain(excel.getQuestionJudgmentExplain());
                questionJudgment.setRightKey(excel.getQuestionJudgmentRightKey());
            }

            if(questionJudgment!=null){
                questionBank.setQuestionJudgment(questionJudgment);
            }

            if(questionFillBlanks!=null){
                questionBank.setQuestionFillBlank(questionFillBlanks);
            }


            if(ObjectUtil.isNotEmpty(questionOption)){
                questionBank.setQuestionOption(questionOption);
            }

            questionBanks.add(questionBank);
        }
        return questionBanks;
    }
}
